package com.lys.controller;

import com.lys.entity.News;
import com.lys.param.NewsParams;
import com.lys.service.NewsService;
import com.lys.service.ProductService;
import com.lys.utils.EmptyUtils;
import com.lys.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/30 8:25
 */
@Controller
@RequestMapping("/admin/news")
public class AdminNewsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private ProductService productService;

    /**
     * 查询新闻列表
     * @return
     */
    @GetMapping("/list")
    public String queryNewsList(HttpServletRequest request,
                                Model model){
        //获取当前页数
        String currentPageStr = request.getParameter("currentPage");
        //获取页大小
        String pageSize = request.getParameter("pageSize");
        int rowPerPage = EmptyUtils.isEmpty(pageSize)?10:Integer.parseInt(pageSize);
        int currentPage = EmptyUtils.isEmpty(currentPageStr)?1:Integer.parseInt(currentPageStr);
        int total=newsService.queryNewsCount(new NewsParams());
        Pager pager=new Pager(total,rowPerPage,currentPage);
        pager.setUrl("admin/news/list");
        List<News> newsList = newsService.getAllNews(pager);
        model.addAttribute("newsList", newsList);
        model.addAttribute("pager", pager);
        model.addAttribute("menu", 7);
        return "/backend/news/newsList";
    }
    /**
     * 查询新闻详情
     * @return
     */
    @GetMapping("/{id}")
    public String newsDetail(@PathVariable("id") String id,
                             Model model){
        News news=newsService.findNewsById(id);
        model.addAttribute("news",news);
        model.addAttribute("menu", 7);
        return "/backend/news/newsDetail";
    }

}
